<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ CMS ]
 */
namespace CMS\Controller;
use Common\Controller\AdminController;

/**
 * 打赏管理
 */
class MoneyController extends AdminController{
	
	/**
	 * 打赏管理
	 */
    public function Manage(){

		//页面配置
		$page = array(
			
			'title'=>'打赏管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
		//开始结束时间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and'); 
			
		}else{
			
			//默认今天明天
			$begin_time=time();
			$end_time=time()+86400;
		}
		
		//判断是否搜索标题
		$value = I('param.value');
		if($value){
			
			$map['value'] = array('like',"%$value%");

		}
		$this->assign('value',$value);
		
		
		
		
		
		
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		$m_cms_content = M('cms_content');
		$m_common_user = M('common_user');
		
		$down_money = I('param.down_money');
		$top_money = I('param.top_money');
		if($down_money != null and $top_money != null){
			
			$map['money']  = array(array('egt',$down_money),array('elt',$top_money),'and'); 
			
			$this->assign('down_money',$down_money);
			$this->assign('top_money',$top_money);
		}
		
		//获得上级信息
		$form_id = I('param.form_id')?I('param.form_id'):0;
		
		
		//获得菜单信息
		if($form_id!=0){
			
			$map['form_id'] = $form_id;
		}
		
		//判断是否搜索状态
		$status = I('param.status');
		if($status){
			
			$map['status'] = $status;

		}else{
			
			$map['status'] = array('in','1,2');
		
			
		}
		$this->assign('status',$status);
		
		$map['type'] = get_cms_model_conf('CMS_MONEY');
		
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		
		$list = $m_cms_content->where($map)->order('status asc,time desc')->page($_GET['p'].',50')->select();
	
		$count      = $m_cms_content->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		//查询关联信息
		foreach($list as $i=>$k){
			
			$fmap['id'] = $list[$i]['form_id'];
			$fdata = $m_cms_content->where($fmap)->find();
			$umap['id'] = $list[$i]['uid'];
			
			$udata = $m_common_user->where($umap)->find();
			
			$tmap['id'] = $fdata['uid'];
			$tdata = $m_common_user->where($tmap)->find();

			
			//用户相关
			$list[$i]['nickname'] = $udata['nickname'];
			$list[$i]['n_img'] = $udata['img'];
			//文章相关
			$list[$i]['channel'] = $fdata['name'];
			$list[$i]['t_nickname'] = $tdata['nickname'];
			$list[$i]['t_img'] = $tdata['img'];
			$list[$i]['t_uid'] = $tdata['id'];
			
		
		}
		//赋值
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('form_id',$fmap['id']);
		
	
		
		
		$this->display();
	}
	
	/**
	 * 编辑打赏
	 */
	public function Edit(){
    
		$m_cms_content = M('cms_content');
		
		$map['id'] = I('param.id');
		//判断是否是ajax请求
		if(IS_POST){

			$m_cms_content = M('cms_content');
			
			set_cms_draft($map['id']); //保存草稿
			
			$data = I('');
			unset($data['id']);
			$data['value'] = I('param.value','','');
			$data['time'] = strtotime(I('param.time'));
			
			$data['ip'] = get_client_ip();
			
			$result = $m_cms_content->where($map)->save($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
			
				$this->success('编辑成功', U('CMS/Evaluate/Manage'));
			
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置  
			
				$this->error('编辑失败');
			
			}
		
		}else{
			
			$map['type'] = get_cms_model_conf('CMS_MONEY');
			$map['status'] = array('in','1,2');
		
		
			$data = $m_cms_content->where($map)->find();
			
			$this->assign('data',$data);
		
			$this->display();
		}
		
	
	}
	
	/**
	 * 批量改变状态
	 */
	public function Delete($id,$status=0){
		
	
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_cms_content = M('cms_content');
		
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					
					$map['id'] = $id[$i];
					$data['status'] = $status;
					
					$m_cms_content->where($map)->save($data);
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	
}